package com.box2dwrapper.object
{
	import Box2D.Collision.Shapes.*;
	import Box2D.Dynamics.b2Body;
	import Box2D.Dynamics.b2BodyDef;
	
	import com.box2dwrapper.view.core.AbstractView;
	
	import flash.display.DisplayObject;
	
	public class AbstractObject
	{
		public var body:b2Body;
		public var bodyDef:b2BodyDef;
		public var polyDef:b2PolygonDef;
		public var circleDef:b2CircleDef;
		public var width:Number;
		public var height:Number;
		
		public function AbstractObject()
		{
		}
		
		public function position(numX:Number = 0, numY:Number = 0):void
		{
			bodyDef.position.Set(numX/AbstractView.SCALE, numY/AbstractView.SCALE);
		}		
		
		public function set skin(displayObject:*):void 
		{ 
			bodyDef.userData = displayObject;
			if(displayObject is DisplayObject){
				bodyDef.userData.width = width * 2;
				bodyDef.userData.height = height * 2;	
			}
			
		}
		
		public function addBody(bodyShape:AbstractObject, setMassFromShape:Boolean = true):void
		{
			if(bodyShape is Box || bodyShape is OrientedBox){
				body.CreateShape(bodyShape.polyDef);
			}else if(bodyShape is Circle){
				body.CreateShape(bodyShape.circleDef);
			}
			if(setMassFromShape) body.SetMassFromShapes();
		}
		
		public function radiansToDegrees(rad:Number):Number
		{
			return rad * 180/Math.PI;
		}
 
		public function degreesToRadians(deg:Number):Number
		{
			return deg * Math.PI/180;
		}
		
		public function set x(numX:Number):void { bodyDef.position.x = numX / AbstractView.SCALE; }
		public function get x():Number { return bodyDef.position.x * AbstractView.SCALE; }
		
		public function set y(numY:Number):void { bodyDef.position.y = numY / AbstractView.SCALE; }
		public function get y():Number { return bodyDef.position.y * AbstractView.SCALE; }

	}
}